package edu.szcu.vitaguard.controller;

import edu.szcu.vitaguard.entity.Doctor;
import edu.szcu.vitaguard.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生管理控制器
 */
@RestController
@RequestMapping("/doctors")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    /**
     * 添加医生
     * @param doctor 医生对象
     * @return 添加结果
     */
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addDoctor(@RequestBody Doctor doctor) {
        Map<String, Object> response = new HashMap<>();
        try {
            Doctor savedDoctor = doctorService.addDoctor(doctor);
            response.put("success", true);
            response.put("message", "医生添加成功");
            response.put("data", savedDoctor);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "医生添加失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 获取所有医生
     * @return 医生列表
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getAllDoctors() {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Doctor> doctors = doctorService.getAllDoctors();
            response.put("success", true);
            response.put("message", "获取医生列表成功");
            response.put("data", doctors);
            response.put("total",doctors.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取医生列表失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 根据ID获取医生
     * @param doctorId 医生ID
     * @return 医生对象
     */
    @GetMapping("/{doctorId}")
    public ResponseEntity<Map<String, Object>> getDoctorById(@PathVariable Integer doctorId) {
        Map<String, Object> response = new HashMap<>();
        try {
            Doctor doctor = doctorService.getDoctorById(doctorId);
            if (doctor != null) {
                response.put("success", true);
                response.put("message", "获取医生信息成功");
                response.put("data", doctor);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "未找到ID为" + doctorId + "的医生");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取医生信息失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 根据科室ID获取医生
     * @param deptId 科室ID
     * @return 医生列表
     */
    @GetMapping("/department/{deptId}")
    public ResponseEntity<Map<String, Object>> getDoctorsByDeptId(@PathVariable Integer deptId) {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Doctor> doctors = doctorService.getDoctorsByDeptId(deptId);
            response.put("success", true);
            response.put("message", "获取科室医生列表成功");
            response.put("data", doctors);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取科室医生列表失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 根据医生姓名查询医生
     * @param name 医生姓名
     * @return 医生列表
     */
    @GetMapping("/search")
    public ResponseEntity<Map<String, Object>> getDoctorsByName(@RequestParam String name) {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Doctor> doctors = doctorService.getDoctorsByName(name);
            response.put("success", true);
            response.put("message", "搜索医生成功");
            response.put("data", doctors);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "搜索医生失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 根据状态查询医生
     * @param status 状态
     * @return 医生列表
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<Map<String, Object>> getDoctorsByStatus(@PathVariable String status) {
        Map<String, Object> response = new HashMap<>();
        try {
            List<Doctor> doctors = doctorService.getDoctorsByStatus(status);
            response.put("success", true);
            response.put("message", "获取指定状态医生列表成功");
            response.put("data", doctors);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取指定状态医生列表失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 更新医生信息
     * @param doctorId 医生ID
     * @param doctor 医生对象
     * @return 更新结果
     */
    @PutMapping("/{doctorId}")
    public ResponseEntity<Map<String, Object>> updateDoctor(@PathVariable Integer doctorId, @RequestBody Doctor doctor) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 确保ID一致
            doctor.setDoctorId(doctorId);

            Doctor updatedDoctor = doctorService.updateDoctor(doctor);
            if (updatedDoctor != null) {
                response.put("success", true);
                response.put("message", "医生信息更新成功");
                response.put("data", updatedDoctor);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "未找到ID为" + doctorId + "的医生");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "医生信息更新失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 删除医生
     * @param doctorId 医生ID
     * @return 删除结果
     */
    @DeleteMapping("/{doctorId}")
    public ResponseEntity<Map<String, Object>> deleteDoctor(@PathVariable Integer doctorId) {
        Map<String, Object> response = new HashMap<>();
        try {
            boolean result = doctorService.deleteDoctor(doctorId);
            if (result) {
                response.put("success", true);
                response.put("message", "医生删除成功");
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "未找到ID为" + doctorId + "的医生");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "医生删除失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
}
